<template>
  <div>
    <el-breadcrumb separator="/" class="breadcrumb">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">我的题库</a></el-breadcrumb-item>
    </el-breadcrumb>
    <div style="display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center;">
      <div class="searchBox">
        <el-input placeholder="搜索题库名" prefix-icon="el-icon-search" v-model="bankName" clearable class="inputBox"></el-input>
        <el-button type="primary" @click="search">搜索</el-button>
      </div>
    </div>
    <div class="content">
      <el-tabs class="tabs" :tab-position="tabPosition" v-model="subject" @tab-click="changeBanks" v-if="showSearch===1">
        <el-tab-pane
          v-for="(item, index) in allSubjects"
          :key="item.id"
          :label="item.name"
          :name="index"
          lazy
        >
          <div class="cards">
            <div v-for="(item, index) in banks" :key="item" @click="goToQuestion(item.id)">
              <el-card class="box-card" >
                <div class="title">{{ item.name }}</div>
                <div class="type" v-if="(index + item.id)%3 == 0">单选题 &nbsp;12道</div>
                <div class="type" v-if="(index + item.id)%3 == 1">单选题 &nbsp;18道</div>
                <div class="type" v-if="(index + item.id)%3 == 2">单选题 &nbsp;10道</div>
                <div class="type" v-if="(index + item.id)%2 == 0">多选题 &nbsp;9道</div>
                <div class="type" v-if="(index + item.id)%2 == 1">多选题 &nbsp;15道</div>
                <div class="type" v-if="(index + item.id)%2 == 2">多选题 &nbsp;11道</div>
                <div class="type" v-if="(index + item.id)%3 == 0">填空题 &nbsp;5道</div>
                <div class="type" v-if="(index + item.id)%3 == 1">填空题 &nbsp;5道</div>
                <div class="type" v-if="(index + item.id)%3 == 2">填空题 &nbsp;5道</div>
                <div class="type" v-if="(index + item.id)%3 == 0">简答题 &nbsp;8道</div>
                <div class="type" v-if="(index + item.id)%3 == 1">简答题 &nbsp;9道</div>
                <div class="type" v-if="(index + item.id)%3 == 2">简答题 &nbsp;10道</div>
                <div class="updatetime">{{ '上次更新：' + item.updateTime }}</div>
              </el-card>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <div v-if="showSearch===0" class="searchResults">
      <div style="width: 10%; padding: 15px;">
        <el-button type="primary" @click="backToTabs">返回</el-button>
      </div>
      <div style="width: 80%;">
        <div class="cards">
          <div v-for="item in searchBanks" :key="item" @click="goToQuestion(item.id)">
            <el-card class="box-card" >
              <div class="title">{{ item.name }}</div>
              <div class="type" v-if="(index + item.id)%3 == 0">单选题 &nbsp;12道</div>
              <div class="type" v-if="(index + item.id)%3 == 1">单选题 &nbsp;18道</div>
              <div class="type" v-if="(index + item.id)%3 == 2">单选题 &nbsp;10道</div>
              <div class="type" v-if="(index + item.id)%2 == 0">多选题 &nbsp;9道</div>
              <div class="type" v-if="(index + item.id)%2 == 1">多选题 &nbsp;15道</div>
              <div class="type" v-if="(index + item.id)%2 == 2">多选题 &nbsp;11道</div>
              <div class="type" v-if="(index + item.id)%3 == 0">填空题 &nbsp;5道</div>
              <div class="type" v-if="(index + item.id)%3 == 1">填空题 &nbsp;5道</div>
              <div class="type" v-if="(index + item.id)%3 == 2">填空题 &nbsp;5道</div>
              <div class="type" v-if="(index + item.id)%3 == 0">简答题 &nbsp;8道</div>
              <div class="type" v-if="(index + item.id)%3 == 1">简答题 &nbsp;9道</div>
              <div class="type" v-if="(index + item.id)%3 == 2">简答题 &nbsp;10道</div>
              <div class="updatetime">{{ '上次更新：' + item.updateTime }}</div>
            </el-card>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavMenu from '../../../components/NavMenu'
import Footer from '../../../components/Footer'
import { findSubjectsByHouse, findBankByName, findBankBySubject } from '../../../api/questionBank.js'
// import { getBanksByHouse } from '../../../api/students.js'

export default {
  name: 'index',
  components: { Footer, NavMenu },
  data () {
    return {
      house: sessionStorage.getItem('house'),
      addDialogVisible: false,
      houseName: '计算机学院',
      tabPosition: 'left',
      bankName: '',
      banks: [],
      searchBanks: [],
      newBank: {
        name: '',
        house: '',
        subjectId: ''
      },
      subject: '',
      showSearch: 1,
      allSubjects: []
    }
  },
  mounted () {
    this.changeBanks()
  },
  methods: {
    changeBanks () {
      // console.log(this.houseName)
      findSubjectsByHouse(this.houseName).then(res => {
        if (res.data.errorCode === 0) {
          this.allSubjects = res.data.data
          console.log(this.subject)
          var sid = this.allSubjects[this.subject].id
          findBankBySubject(0, 20, sid).then(res => {
            if (res.data.errorCode === 0) {
              this.banks = res.data.data.list
            } else {
              this.$message.error(res.data.message)
            }
          })
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    goToQuestion (id) {
      console.log(id)
      this.$router.push({path: '/student/question', query: {id: id}})
    },
    search () {
      findBankByName(0, 10, this.bankName).then(res => {
        if (res.data.errorCode === 0) {
          var temp = res.data.data.list
          var rtemp = []
          this.showSearch = 0
          for (var item of temp) {
            if (this.allSubjects.indexOf(item.subject)) {
              rtemp.push(item)
            }
          }
          this.searchBanks = rtemp
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    backToTabs () {
      this.showSearch = 1
      this.bankName = ''
    },
    chooseSubject () {
      findSubjectsByHouse(this.newBank.house).then(res => {
        if (res.data.errorCode === 0) {
          console.log(res.data)
          this.chooseSubjects = res.data.data
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  }
}
</script>

<style scoped>
  /* eslint-disable */
  .content {
    padding: 15px;
    height: calc(100% - 60px);
    width: calc(100% - 30px);
    /* background-color: #42B983; */
    display: flex;
    display: -webkit-flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
  .breadcrumb {
    padding: 20px;
  }
  .searchBox {
    display: flex;
    margin: 10px 50px;
  }
  .inputBox {
    width: 300px;
    margin-right: 20px;
  }
  .box-card {
    width: 200px;
    margin: 10px 10px 30px;
    box-shadow:0 0 8px rgba(98, 98, 98, 0.5);
  }
  .box-card:hover{
    /* width: 305px; */
    transform: scale(1.02);
    box-shadow:0 0 10px rgba(117, 179, 255, 0.7);
  }
  .title{
    font-weight: bold;
    font-size: 20px;
    margin-bottom: 18px;
    text-align: left;
    padding-bottom: 10px;
    border-bottom: solid 1px rgba(117, 179, 255, 0.7);
  }
  .title:hover{
    /* transform: scale(1.004); */
    font-size: 21px;
    cursor: pointer;
    color: #123456;
  }
  .type {
    font-size: 16px;
    margin-bottom: 10px;
    color: #555555;
    display: block;
  }
  .updatetime {
    float: right;
    font-size: 12px;
    color: #919698;
    margin-top: 10px;
    margin-bottom: 8px;
  }
  .tabs {
    width: 90%;
  }

  /deep/ .el-tabs__nav {
    z-index: 0;
  }

  /deep/ .el-tabs--left .el-tabs__item.is-left {
    font-size: 20px;
    height: 60px;
    line-height: 60px;
    border-radius: 20px 0 0 20px;
    width: 150px;
    margin-right: 0px;
  }
  /deep/ .el-tabs--left .el-tabs__item:hover {
    width: 155px;
    color: #004100;
    font-weight: bold;
    box-shadow: 0px 12px 8px -12px rgba(98, 98, 98, 0.5);
  }

  /deep/ .el-tabs--left .el-tabs__item.is-left.is-active {
    width: 155px;
    font-weight: bold;
    color: #004100;
    background-color: rgba(170, 255, 127, 0.4);
    box-shadow: 5px 12px 8px -10px rgba(98, 98, 98, 0.5);
    border: 2px solid rgba(11, 11, 55, 0.3);
  }

  /deep/ .el-tabs--left .el-tabs__active-bar {
    background-color: #123456;
  }
  .cards {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    margin: 0 20px;
  }
  .searchResults {
    display: flex;
    width: 90%;
    margin: 0 auto;
  }
</style>
